<?php

class MallShopController extends Controller
{

    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = MallShop::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = MallShop::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $logo = Util::getParamNotEmpty("logo");
            $mobile = Util::getParamNotEmpty("mobile");
            $address = Util::getParamNotEmpty("address");


            //添加
            $obj = new MallShop();
            Util::initObjData($obj);


            $obj->name = $name;
            $obj->logo = $logo;
            $obj->mobile = $mobile;
            $obj->address = $address;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");
            $logo = Util::getParamNotEmpty("logo");
            $mobile = Util::getParamNotEmpty("mobile");
            $address = Util::getParamNotEmpty("address");


            //事务开始
            $connection = Yii::app()->db;

            $transaction = $connection->beginTransaction();

            try {

                //验证存在
                $obj = MallShop::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                //修改信息

                $obj->name = $name;
                $obj->logo = $logo;
                $obj->mobile = $mobile;
                $obj->address = $address;

                //保存
                Util::initObjData($obj);
                Util::saveObjData($obj);

                //修改商铺的同时修改商品对应的信息
                $searchsql = Util::initSearchSql();

                $searchsql = $searchsql. " and shop_id = " .$obj->shop_id;

                $list = MallGoods::model()->findAll($searchsql);

                foreach($list as $value){

                    $value->shop_name = $name;

                    $detail_info = json_decode($value->detail_info);
                    $detail_info -> shop_name = $name;
                    $detail_info = json_encode($detail_info);

                    $value->detail_info = $detail_info;

                    Util::saveObjData($value);
                }





                $transaction->commit();//事务结束
            } catch (Exception $e) {
                $transaction->rollback();//回滚函数

                throw new Exception($e->getMessage());

            }

            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = MallShop::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

}